package com.example.sensornodemonitor.app;

import android.app.ListFragment;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.os.Bundle;
import android.view.*;
import android.widget.*;

import java.util.*;

/**
 * Created by mattiasbowallius on 24/02/14.
 */
public class FoundDevicesFragment extends ListFragment {

    private static final String ARG_SECTION_NUMBER = "section_number";
    private BluetoothLowEnergyActivity context;
    private BluetoothListAdapter listAdapter;

    public static FoundDevicesFragment newInstance(BluetoothLowEnergyActivity context, int sectionNumber) {
        FoundDevicesFragment fragment = new FoundDevicesFragment(context);
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public FoundDevicesFragment(BluetoothLowEnergyActivity context) {
        listAdapter = new BluetoothListAdapter(context, R.layout.device_row_layout, new BluetoothDevice[0]);
        setListAdapter(listAdapter);
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_bluetooth_low_energy, container, false);
        Button scanButton = (Button) rootView.findViewById(R.id.scan_button);
        scanButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listAdapter.clear();
                listAdapter.notifyDataSetChanged();
                ((BluetoothLowEnergyActivity) getActivity()).scanLeDevice(true);
            }
        });
        return rootView;
    }

    @Override
    public void onListItemClick(ListView l, View v, int pos, long id) {
        context.connectToDevice(listAdapter.getItem(pos));
    }

    public class BluetoothListAdapter extends ArrayAdapter<BluetoothDevice> {

        private ArrayList<BluetoothDevice> devices;

        public BluetoothListAdapter(Context context, int resource, BluetoothDevice[] objects) {
            super(context, resource, objects);
            devices = new ArrayList<>();
            devices.addAll(Arrays.asList(objects));
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.device_row_layout, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.label);
            textView.setText(devices.get(position).getAddress());
            return rowView;
        }

        public void addDevice(BluetoothDevice device) {
            if (!devices.contains(device)) {
                devices.add(device);
            }
        }

        @Override
        public BluetoothDevice getItem(int pos){
            return devices.get(pos);
        }

        @Override
        public int getCount() {
            return devices.size();
        }

        @Override
        public void clear() {
            devices.clear();
        }
    }
}
